package cn.qiyu5522.utils;

import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

/**
 * @BelongsProject: tk_ems
 * @BelongsPackage: cn.qiyu5522.tk_ems.utils
 * @Author: TangQi
 * @CreateTime: 2023-02-06  09:38
 * @Description: TODO
 * @Version: 1.0
 */
public class TraceIdUtils {

    public static final String TRACE_ID_KEY = "requestId";

    public static String generateTranceId(){

//        UUID.randomUUID().toString().replace("-", "")
        return IdUtil.fastSimpleUUID().toString();
    }

    public static void setTraceId(String traceId) {
        if (StringUtils.isNotEmpty(traceId)) {
            MDC.put(TRACE_ID_KEY, traceId);
        }
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID_KEY);
    }

    public static void removeTraceId() {
        MDC.remove(TRACE_ID_KEY);
    }

    public static void clearTraceId() {
        MDC.clear();
    }

}

